<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;

$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$return=array();
$return['basic']=$this->plugin_setting('basic');
$vip=$this->plugin_setting('vip');
$return['vip']=$vip;
$return['banner']=$basic['image'];
if($_RQ['op']=='main'){
	$post=array();
	$post['paytype']=1;
	$post['num']=1;
	$post['checked']=true;
	$return['systemxieyi']=$this->plugin_setting('systemxieyi');
	if($_RQ['postdata']){
		$postdata=get_postdata();
		$packnum=$postdata['num'];
		$packname=$postdata['paytype'];
		if($packname==1){
			$fee=$vip['month']*$packnum;
		}elseif($packname==2){
			$fee=$vip['quarter']*$packnum;	
		}elseif($packname==3){
			$fee=$vip['year']*$packnum;
		}
		if($postdata['fileddata']){
			foreach ($postdata['fileddata'] as $item) {
				if($item['ismust']==1 && empty($item['data'])){
					exi('请完善'.$item['title'].'内容后再提交','error');
				}
			}
		}
		//创建一个会员订单
		$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$_SESSION['uid'],
			'packnum'=>$packnum,
			'ordersn'=>get_ordersn('jy_yuyue_viporder','ordersn'),
			'fee'=>$fee,
			'packname'=>$packname,
			'ispay'=>0,
			'status'=>0,
			'createtime'=>TIMESTAMP,
		);
		pdo_insert('jy_yuyue_viporder',$insert);
		$id=pdo_insertid();
		if($postdata['fileddata']){
			foreach ($postdata['fileddata'] as $item) {
				$data=array(
				'uniacid'=>$_SESSION['uniacid'],
				'mid'=>$_SESSION['uid'],
				'order_id'=>$id,
				'title'=>$item['title'],
				'type'=>$item['type'],
				'createtime'=>TIMESTAMP,
				);
				if($item['type']==7){
					$data['data']=$item['data']['province'].$item['data']['city'].$item['data']['district'].$item['data']['xiangxi'];
					
				}
				else{
					$data['data']=$item['data'];
				}
			pdo_insert('jy_yuyue_vip_data',$data);
			}
		
		}
		$params['fee']=$fee;
		$params['title']='激活预约订单';
		$params['id']=$id;
		$params['plugin']='jy_yuyue';
		//是否关闭余额支付
		if($return['basic']['iscredit']==1){
			$params['pay_type']='wechat';//可选，此项作用为指定支付方式
			$params['no_type']='credit';//可选，此项作用为指定不允许使用哪种支付方式
		}
		$params['back_url']='jy_yuyue/buyvip/payresult/';
 		$return['payid']=cfc('pay')->create_pay_order($params);
	}
	$return['post']=$post;
	$vip=$this->plugin_setting('vip');
	$return['vip']=$vip;
	$vip_fileds=$this->plugin_setting('vip_fileds');
	$return['post']['fileddata']=$vip_fileds['vip_fileds'];
	//print_r($return['fileddata']);exit;
	$return['fee']=price($vip['month']);
	$return['fee0']=$vip['month0']?price($vip['month0']):price($vip['month']*1.5);
	exi($return);
}

if($_RQ['op']=='payresult'){
	$payresult=$_RQ['pay_result'];
	if(!$payresult){
		exi('支付失败，未收到支付结果！','error');
	}
	if($payresult['status']!=1){
		exi('支付失败！','error');
	}
	$order=pdo_get('jy_yuyue_viporder',array('id'=>$payresult['id'],'uniacid'=>$_SESSION['uniacid']));
	if(!$order){
		exi('支付失败！购买会员订单不存在！','error');
	}
	if($order['fee']!=$payresult['fee']){
		exi('支付失败！购买会员订单金额不匹配！','error');
	}
	pdo_update('jy_yuyue_viporder',array('ispay'=>1,'fee'=>$payresult['fee']),array('id'=>$order['id']));
	//计算到期时间
	$startTime =time();
	$user=pdo_get('jy_yuyue_user',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['mid']));
	if($order['packname']==3){
		$order['packnum']=$order['packnum']*12;
	}elseif($order['packname']==2){
		$order['packnum']=$order['packnum']*3;
	}
	if($user['viptime']!=0){
		
		$addTime=$user['viptime']+$order['packnum']*30*24*3600;
	}else{
		$addTime =$startTime+$order['packnum']*30*24*3600;
	}
	pdo_update('jy_yuyue_user',array('isvip'=>1,'viptime'=>$addTime),array('id'=>$order['mid'],'uniacid'=>$_SESSION['uniacid']));
	//发送模板消息
	//$this->notice('buyvip',$_SESSION['uid'],'',$order['id']);
	exi('支付成功','success','jy_yuyue/home/main/');
}